package br.com.cast.syscast.municipio.paginas;

import java.util.ArrayList;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.util.ListModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.validation.validator.StringValidator;

import br.com.cast.syscast.HomePage;
import br.com.cast.syscast.excecoes.MunicipioJaCadastrado;
import br.com.cast.syscast.models.Municipio;
import br.com.cast.syscast.models.Pais;
import br.com.cast.syscast.models.Uf;
import br.com.cast.syscast.services.IMunicipioService;
import br.com.cast.syscast.services.IPaisService;
import br.com.cast.syscast.services.UfService;
import br.com.cast.syscast.util.ComponentUtil;

public class FormMunicipio extends Form<Municipio> {

	private static final long serialVersionUID = 1L;

	@SpringBean
	private UfService ufService;

	@SpringBean
	private IPaisService paisService;
	
	@SpringBean
	private IMunicipioService municipioService;

	private DropDownChoice<Uf> dropDownUf;

	public FormMunicipio(String id) {
		super(id, new CompoundPropertyModel<Municipio>(new Municipio()));

		criaCampoPais();
		
		criaCampoUf();
		
		criaCampoNome();
		
		criaCampoPopulacao();
		
		criaCampoPIB();

		add(new AjaxButton("salvar") {
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				
				Municipio municipio = (Municipio) form.getDefaultModelObject();
				HomePage template = ComponentUtil.getTemplate(this);

				try {
					municipioService.salvar(municipio);
					redirecionaListar(target, template);
					template.success("Registro inserido com sucesso!");
				} catch (MunicipioJaCadastrado e) {
					error(e.getMessage());
				}
				
				target.add(template.get("msgErro"));
				target.add(template.get("msgSucesso"));
				
			}
			
			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {
				HomePage template = ComponentUtil.getTemplate(this);
				target.add(template.get("msgErro"));
				target.add(template.get("msgSucesso"));
			}
			
		});
		
		criaLinkCancelar();
	}

	private void criaCampoPIB() {
		add(new RequiredTextField<>("pib"));
	}

	private void criaCampoPopulacao() {
		add(new RequiredTextField<>("populacao"));
	}

	private void criaCampoNome() {
		add(new RequiredTextField<>("nome")
				.add(new StringValidator(0, 50)));
	}

	private void criaCampoPais() {
		IChoiceRenderer<Pais> renderer = new ChoiceRenderer<>("nome");
		
		ListModel<Pais> model = new ListModel<>(paisService.consultaTodos());
		
		final DropDownChoice<Pais> pais = new DropDownChoice<>(
				"uf.pais", model, renderer);
		
		pais.add(new AjaxFormComponentUpdatingBehavior("change") {

			@Override
			protected void onUpdate(AjaxRequestTarget target) {
				Pais paisSelecionado = (Pais) pais.getDefaultModelObject();
				dropDownUf.setChoices(ufService.consultaPorPais(paisSelecionado));
				target.add(dropDownUf);
			}
		});
		
		add(pais);
	}
	
	private void criaCampoUf() {
		IChoiceRenderer<Uf> renderer = new ChoiceRenderer<>("siglaNome");
		dropDownUf = new DropDownChoice<>(
				"uf", new ArrayList<Uf>(), renderer);
		dropDownUf.setOutputMarkupId(true);
		add(dropDownUf);
	}

	private void criaLinkCancelar() {
		add(new AjaxLink("cancelar") {

			@Override
			public void onClick(AjaxRequestTarget target) {
				HomePage template = ComponentUtil.getTemplate(this);
				redirecionaListar(target, template);
			}
		});
	}

	/*@Override
	protected void onSubmit() {
		Municipio municipio = (Municipio) getDefaultModelObject();

		try {
			municipioService.salvar(municipio);
			HomePage template = redirecionaListar();
			template.success("Registro inserido com sucesso!");
		} catch (MunicipioJaCadastrado e) {
			error(e.getMessage());
		}
	}*/

	private void redirecionaListar(AjaxRequestTarget target, HomePage template) {
		ListarMunicipio listar = new ListarMunicipio(HomePage.ID_CONTEUDO);
		template.replace(listar);
		target.add(listar);
	}

}
